import socket
import os

# 连接服务器
# server_ip = '10.211.65.250'
server_ip = '47.119.40.164'
server_port = 39000

# 打开图片并读取为字节流
image_path = r"C:\Users\Nick\Desktop\photo.jpg"
with open(image_path, 'rb') as f:
    image_data = f.read()

# 发送文件名和文件数据
filename = os.path.basename(image_path)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((server_ip, server_port))

    # 发送文件名的长度和文件名
    s.sendall(len(filename).to_bytes(4, byteorder='big'))
    s.sendall(filename.encode('utf-8'))

    # 发送文件大小
    s.sendall(len(image_data).to_bytes(4, byteorder='big'))

    # 发送文件数据
    s.sendall(image_data)

    print(f"Image {filename} upload.")